/**
 * Created by Admin on 2017/7/3.
 */
/*隐藏模态框*/
function closeModal() {
    $(".modal .flex").click(function () {
        $(this).parent().removeClass("block");
        $("body").removeClass("no-scroll");
        $("html").removeClass("no-scroll");
        $(".container").removeClass("no-scroll");
    });

    $(".close").click(function () {
        $(this).parents(".modal").removeClass("block");
        $("body").removeClass("no-scroll");
        $("html").removeClass("no-scroll");
        $(".container").removeClass("no-scroll");
    });

    $(".confirm-btn").click(function(){
        $(this).parents(".modal").removeClass("block");
    })
}
closeModal();

$(".modal-content").click(function (e) {
    e.stopPropagation ? e.stopPropagation() : e.cancelBubble = true;
});

/*显示模态框*/
function showModal(el) {
    $(el).addClass("block");
    $("body").addClass("no-scroll");
    $("html").addClass("no-scroll");
    $(".container").addClass("no-scroll");
}

function subBind() {
    $("#btn-bind").click(function () {
        var uname =  false;
        var upwd = false;
        if($("#u-name").val()==""){
            $("#u-name").next().addClass('block').html("The user name cannot be empty");
            uname =  false;
        }else{
            uname =  true;
        }
        if($("#u-pwd").val()==""){
            $("#u-pwd").next().addClass('block').html("The password cannot be empty");
            upwd =  false;
        }else{
            upwd =  true;
        }
        if(uname && upwd){
            var  data= {};
             var str = $(".form-bind").serialize();
             str = str.split("&");
             for(var i=0,arr;i<str.length;i++){
                 arr = str[i].split("=");
                 data[arr[0]] = arr[1];
             }
            //CF08760A-D4FA-4A21-8151-6B59FC44D92A 44C02BF6-1D02-4B18-8613-26F26405CAE4
             data.appid = '44C02BF6-1D02-4B18-8613-26F26405CAE4';
             data.openid = localStorage['openid'];
             data.language = "EN";
             data = JSON.stringify(data);
             loading.show();
             httpReq("system/wechat_user_bind", data, function (res) {
                 loading.hidden();
                 console.log(res);
                 if(res.return_code =="SUCCESS"){
                     sessionStorage['merchant'] = JSON.stringify(res);
                 }
                 $("#login_modal").removeClass("block");
                 location.href = "main.html";
             }, function (err) {
                 if($("#myLoading")){
                     loading.hidden();
                 }
                 err.responseText = JSON.parse(err.responseText);
                 $("#login_modal").removeClass("block");
                 $("body").removeClass("no-scroll");
                 $("html").removeClass("no-scroll");
                 $(".container").removeClass("no-scroll");
                 swal({
                     title: "Error",
                     text: err.responseText.error_msg,
                     type: "error",
                     animation: "slide-from-top"
                 });
             })
        }
    });
}
subBind();

function removeHelp(el){
    if($(el).val()!==""){
        $(el).next().removeClass("block")
    }
}

function subReg() {

    $("#btn-sub").click(function(){
        var trade = false,company = false,industry = false,addr = false,contact = false,phone = false,email=false;
        if($("#t-name").val()==""){
            $("#t-name").next().addClass('block').html("The trading name cannot be empty");
            trade = false;
        }else{trade = true;}
        if($("#c-name").val()==""){
            $("#c-name").next().addClass('block').html("The company name cannot be empty");
            company = false;
        }else{company = true;}
        if($("#type").val()==""){
            $("#type").next().addClass('block').html("The industry cannot be empty");
            industry = false;
        }else{industry = true;}
        if($("#addr").val()==""){
            $("#addr").next().addClass('block').html("The address cannot be empty");
            addr = false;
        }else{addr = true;}
        if($("#contact").val()==""){
            $("#contact").next().addClass('block').html("The contact person cannot be empty");
            contact = false;
        }else{contact = true;}
        if($("#phone").val()==""){
            $("#phone").next().addClass('block').html("The phone cannot be empty");
            phone = false;
        }else{phone = true;}
        if($("#email").val()==""){
            $("#email").next().addClass('block').html("The email cannot be empty");
            email = false;
        }else{email = true;}
        if(trade && company && industry && addr && contact && phone && email){
            var data = {};
            var str = $(".form-reg").serialize();
            str = decodeURIComponent(str,true);
            str = str.split("&");
            for(var i=0,arr;i<str.length;i++){
                arr = str[i].split("=");
                data[arr[0]] = arr[1];
            }
            data.appid = '44C02BF6-1D02-4B18-8613-26F26405CAE4';
            data.wechat_openid = localStorage['openid'];
            data = JSON.stringify(data);
            loading.show();
            httpReq("merchant/merchant_register", data, function (res) {
                loading.hidden();
                $("#reg_modal").removeClass("block");
                swal({
                    title: "Success",
                    text: "Submit successful, pending review",
                    type: "success",
                    animation: "slide-from-top"
                })
            }, function (err) {
                if($("#myLoading")){
                    loading.hidden();
                }
                err.responseText = JSON.parse(err.responseText);
                $("#reg_modal").removeClass("block");
                $("body").removeClass("no-scroll");
                $("html").removeClass("no-scroll");
                $(".container").removeClass("no-scroll");
                swal({
                    title: "Error",
                    text: err.responseText.error_msg,
                    type: "error",
                    animation: "slide-from-top"
                });
            });
        }

    })

}
subReg();

/*
function httpReq(url, data, succFun, errFun) {
    //120.76.216.91
    var api = "http://47.91.42.63/API/";
    $.ajax({
        type: "POST",
        url: api + url,
        data: data,
        dataType: 'json',
        contentType: "application/json",
        success: function (result) {
            return typeof succFun == 'function' ? succFun(result) : succFun;
        },
        error: function (err) {
            return typeof errFun == 'function' ? errFun(err) : errFun;
        }
    })
}*/
